package org.digitarts.rtms.spring.collectors.shell;

import java.util.Set;

import org.digitarts.rtms.spring.collectors.CollectorBase;
import org.digitarts.rtms.spring.collectors.shell.commands.CommandSet;
import org.digitarts.rtms.tracing.Trace;


public class ShellCollector extends CollectorBase
{
	protected IShell		shell		= null;
	protected CommandSet	commandSet	= null;

	public ShellCollector()
	{
		super();
		this.collectorType = "ShellCollector";
	}

	@Override
	public void collectCallback() throws Exception
	{
		try
		{
			long start = System.currentTimeMillis();
			if (log.isDebugEnabled())
				log.debug("\n\t" + objectName + " Collecting....");
			Set<Trace> traces = commandSet.processCommands(shell);
			for (Trace t : traces)
			{
				t.trace(this.tracer, this.tracingNameSpace);
			}
			long elapsed = System.currentTimeMillis() - start;
			if (log.isDebugEnabled())
				log.debug("\n\t" + objectName + " Collection Complete in " + elapsed + " ms.");
		}
		catch (Exception e)
		{
			if (logErrors)
			{
				log.error("Failed to collect [" + objectName + "]", e);
			}
		}

	}

	public IShell getShell()
	{
		return shell;
	}

	public void setShell(IShell shell)
	{
		this.shell = shell;
	}

	public String toString()
	{
		final String TAB = "    ";

		StringBuilder retValue = new StringBuilder();

		retValue.append("ShellCollector ( \n\t").append("scheduler = ").append(this.scheduler).append(TAB).append(" )");

		return retValue.toString();
	}

	public CommandSet getCommandSet()
	{
		return commandSet;
	}

	public void setCommandSet(CommandSet commandSet)
	{
		this.commandSet = commandSet;
	}

}
